﻿using System;
using System.Data;
using System.Windows.Forms;

namespace Stote.Forms
{
    public partial class FrmProductAddAndEdit : BaseForm
    {
        Dong.BLL.Product productBLL = new Dong.BLL.Product();
        Dong.Model.Product editModel = null;
        int editId = 0;
        public FrmProductAddAndEdit(int id, Form parent)
        {
            InitializeComponent();
            btnAdd.Click += BtnAdd_Click;
            txtMinTemperature.KeyPress += TxtMinTemperature_KeyPress;
            txtMaxTemperature.KeyPress += TxtMaxTemperature_KeyPress;

            if (id == 0)
            {
                editId = 0;
                editModel = null;
                WinText = "添加";
                btnAdd.Text = "添加";
                btnAdd.Image = Properties.Resources.add;
            }
            else
            {
                editId = id;
                editModel = productBLL.GetModel(editId);
                WinText = "编辑";
                btnAdd.Text = "保存";
                btnAdd.Image = Properties.Resources.save;

                txtProductName.Text = editModel.ProductName;
                txtMinTemperature.Text = editModel.MinTemperature.ToString();
                txtMaxTemperature.Text = editModel.MaxTemperature.ToString();
                rtbRemark.Text = editModel.Remark;
            }
        }

        #region 允许输入数字和小数点
        private void TxtMinTemperature_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
            // 允许输入数字和小数点
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                // 如果输入的不是数字或小数点，则取消输入
                e.Handled = true;
            }
            // 不允许小数点重复出现
            if (textBox.Text.Contains(".") && e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }

        private void TxtMaxTemperature_KeyPress(object sender, KeyPressEventArgs e)
        {
            System.Windows.Forms.TextBox textBox = sender as System.Windows.Forms.TextBox;
            // 允许输入数字和小数点
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar) && e.KeyChar != '.')
            {
                // 如果输入的不是数字或小数点，则取消输入
                e.Handled = true;
            }
            // 不允许小数点重复出现
            if (textBox.Text.Contains(".") && e.KeyChar == '.')
            {
                e.Handled = true;
            }
        }
        #endregion

        private void BtnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(txtProductName.Text.Trim()))
                {
                    MessageBox.Show("请输入产品名称！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (string.IsNullOrEmpty(txtMinTemperature.Text.Trim()))
                {
                    MessageBox.Show("请输入产品适应的最小温度！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (string.IsNullOrEmpty(txtMaxTemperature.Text.Trim()))
                {
                    MessageBox.Show("请输入产品适应的最大温度！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (Convert.ToDecimal(txtMinTemperature.Text.Trim()) > Convert.ToDecimal(txtMaxTemperature.Text.Trim()))
                {
                    MessageBox.Show("温度最小值不能大于温度最大值！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    return;
                }

                if (editId == 0)
                {
                    DataSet ds = productBLL.GetList($" Status=0 AND ProductName='{txtProductName.Text.Trim()}'");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show($"此产品名称{txtProductName.Text.Trim()}已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    string now = DateTime.Now.ToString("yyyyMMdd");
                    int currentDateMaxId = 1;
                    DataSet ds2 = productBLL.GetList($" convert(varchar,CreateTime,112)='{now}'");
                    int existCount = ds2.Tables[0].Rows.Count;
                    if (existCount > 0)
                    {
                        currentDateMaxId = existCount + 1;
                    }

                    Dong.Model.Product model = new Dong.Model.Product()
                    {
                        ProductNo= now + currentDateMaxId.ToString().PadLeft(8, '0'),
                        ProductName = txtProductName.Text.Trim(),
                        MinTemperature = Convert.ToDecimal(txtMinTemperature.Text.Trim()),
                        MaxTemperature = Convert.ToDecimal(txtMaxTemperature.Text.Trim()),
                        Remark = rtbRemark.Text.Trim(),
                        CreateUserId = FrmLogin.CurrentUser.UserId,
                    };

                    productBLL.Add(model);
                }
                else
                {
                    DataSet ds = productBLL.GetList($" Status=0 AND ProductName='{txtProductName.Text.Trim()}' AND ProductId!={editModel.ProductId}");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        MessageBox.Show($"修改后的产品名称{txtProductName.Text.Trim()}已存在！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        return;
                    }

                    editModel.ProductName = txtProductName.Text.Trim();
                    editModel.MinTemperature = Convert.ToDecimal(txtMinTemperature.Text.Trim());
                    editModel.MaxTemperature = Convert.ToDecimal(txtMaxTemperature.Text.Trim());
                    editModel.Remark = rtbRemark.Text.Trim();
                    editModel.LastUpdateUserId = FrmLogin.CurrentUser.UserId;
                    editModel.LastUpdateTime = DateTime.Now;
                    productBLL.Update(editModel);
                }
                DialogResult = DialogResult.OK;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
        }
    }
}
